package com.yjha.lzmarathonsys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yjha.lzmarathonsys.bean.TbComment;
import com.yjha.lzmarathonsys.bean.TbUserInfo;
import com.yjha.lzmarathonsys.enums.RedisKeyEnum;
import com.yjha.lzmarathonsys.service.TbCommentService;
import com.yjha.lzmarathonsys.service.TbPersonClockinService;
import com.yjha.lzmarathonsys.service.TbSpecialClockinService;
import com.yjha.lzmarathonsys.service.TbUserInfoService;
import com.yjha.lzmarathonsys.utils.JwtUtils;
import com.yjha.lzmarathonsys.utils.PageUtils;
import com.yjha.lzmarathonsys.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Set;


/**
 * 评论表
 *
 * @author æ­å°
 * @email 1971240051@qq.com
 * @date 2025-05-13 15:07:07
 */
@Api(tags = "评论管理")
@RestController
@RequestMapping("lzmarathonsys/tbcomment")
// @CrossOrigin
public class TbCommentController {
    @Autowired
    private TbCommentService tbCommentService;

    @Autowired
    private TbPersonClockinService tbPersonClockinService;

    @Autowired
    private TbSpecialClockinService tbSpecialClockinService;

    @Autowired
    private TbUserInfoService tbUserService;


    @PostMapping("/add")
    @ApiOperation("发表评论")
    public R addComment(@RequestBody TbComment comment,HttpServletRequest request) {
        return tbCommentService.addComment(comment,request);
    }

    @PostMapping("/auditList")
    @ApiOperation("评论审核列表")
    public R auditCommentList(@RequestParam Map<String, Object> params) {
        PageUtils page = tbCommentService.queryPage(params);

        return R.ok().put("page", page);
    }

    @GetMapping("/audit")
    @ApiOperation("审核评论")
    public R auditComment(@RequestParam Integer tbCommentId, @RequestParam Integer type,  @RequestParam Integer status){
        return tbCommentService.auditComment(tbCommentId,type, status);
    }


    @GetMapping("/getComment")
    @ApiOperation("获取对应打卡评论树")
    public R getComment(@RequestParam("tbTargetId") Integer tbTargetId,  @RequestParam("type") Integer type , HttpServletRequest request){
        return tbCommentService.getCommentTree(tbTargetId,type,request);
    }

    @GetMapping("/getCommentInfo")
    @ApiOperation("获取对应评论详细信息")
    public R getCommentInfo(Integer tbTargetId){
        TbComment comment = tbCommentService.getById(tbTargetId);
        if (comment == null){
            return R.ok("暂无评论");
        }
        comment.setTbUserInfo(tbUserService.getOne(new QueryWrapper<TbUserInfo>().eq("tb_wxuuid",comment.getTbUserId())));
        if (comment.getTbType() == 0){
            comment.setTbPersonClockin(tbPersonClockinService.getById(comment.getTbTargetId()));
        }else{
            comment.setTbSpecialClockin(tbSpecialClockinService.getById(comment.getTbTargetId()));
        }

        return R.ok().put("comment", comment);
    }

    @GetMapping("/deleteComment/{commentId}/{type}")
    @ApiOperation("删除评论")
    public R deleteComment(@PathVariable("commentId") Integer commentId,  @PathVariable("type") Integer type, HttpServletRequest request) {
        return tbCommentService.deleteComment(commentId,type,request);
    }


}
